home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue50 / IPC / Named Pipes / Delphi / ParentMainFormUnit.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-28  |  2.0 KB  |  80 lines

  1. unit ParentMainFormUnit;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   Menus, StdCtrls, ExtCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Memo1: TMemo;
  12.     procedure FormCreate(Sender: TObject);
  13.     procedure FormDestroy(Sender: TObject);
  14.   private
  15.     PipeRead: THandle;
  16.   end;
  17.  
  18. {$ifdef Ver90}
  19.   //This exception class did not exist in Delphi 2
  20.   EWin32Error = class(Exception);
  21. {$endif}
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. const
  27.   PipeNameFixedPrefix = '\\.\pipe\';
  28.   PipeName = PipeNameFixedPrefix + 'SampleNamedPipe';
  29.   PipeMaxInstances = 1; //only allow 1 pipe
  30.   PipeOutBufferSize = 0; //any size
  31.   PipeInBufferSize = 0; //any size
  32.   PipeTimeout = 0; //don't wait
  33.  
  34. implementation
  35.  
  36. uses
  37.   ParentNamedPipeUnit;
  38.  
  39. {$R *.DFM}
  40.  
  41. function LaunchChildApp: THandle;
  42. const
  43.   ChildApp = 'ChildNamedPipe.Exe';
  44. var
  45.   SI: TStartupInfo;
  46.   PI: TProcessInformation;
  47. begin
  48.   GetStartupInfo(SI);
  49.   if not CreateProcess(nil, ChildApp, nil, nil, False,
  50.      0, nil, nil, SI, PI) then
  51.     raise EWin32Error.Create('Unable to launch ' + ChildApp);
  52.   Result := PI.HProcess
  53. end;
  54.  
  55. {This will create a named pipe and get a read and write handle
  56. back. The intention is for a child app to write to the pipe and for
  57. this parent app to read from the pipe.}
  58. procedure TForm1.FormCreate(Sender: TObject);
  59. const
  60.   PipeBufferSize = 0; //default size
  61. begin
  62.   PipeRead := CreateNamedPipe(PipeName, Pipe_Access_Inbound, Pipe_Type_Byte,
  63.     PipeMaxInstances, PipeOutBufferSize, PipeInBufferSize, PipeTimeOut, nil);
  64.   if PipeRead = Invalid_Handle_Value then
  65.     raise EWin32Error.Create('Cannot create named pipe');
  66.   //This launches the child process and waits for it to
  67.   //settle down before moving on to the next statement
  68.   WaitForInputIdle(LaunchChildApp, Infinite);
  69.   //Start reader thread off
  70.   with TTestNamedPipe.Create(PipeRead) do
  71.     FreeOnTerminate := True
  72. end;
  73.  
  74. procedure TForm1.FormDestroy(Sender: TObject);
  75. begin
  76.   FileClose(PipeRead);
  77. end;
  78.  
  79. end.
  80.